package com.faxsun.core.readonly.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;

import mtons.modules.persist.impl.DaoImpl;

import com.faxsun.core.readonly.dao.CustomerDao;
import com.faxsun.core.readonly.entity.CustomerRO;

/**
 * 仅供{@link SessionLoginInterceptor}使用
 * 
 * @author songdragon 2015年6月11日
 *
 */
public class CustomerDaoImpl extends DaoImpl<CustomerRO> implements CustomerDao {

	private static final Logger log = LoggerFactory
			.getLogger(CustomerDaoImpl.class);

	/**
	 * 
	 */
	private static final long serialVersionUID = 6154216426556588297L;

	protected CustomerDaoImpl() {
		super(CustomerRO.class);
	}

	@Override
	@Transactional
	public CustomerRO findById(Long id) {
		CustomerRO customer = null;
		if (id != null && id > 0L) {
			Criteria c = this.createCriteria();
			c.add(Restrictions.eq("id", id));
			c.add(Restrictions.eq("deactivated", false));
			@SuppressWarnings("unchecked")
			List<CustomerRO> list = c.list();
			if (list.size() > 0) {
				if (list.size() > 1) {
					log.error("There are more than one customer with the same Id"
							+ id);
				}
				customer = list.get(0);
			}

		}
		return customer;
	}

}
